package com.cxj.g8llk;

import org.loon.framework.android.game.action.sprite.Animation;
import org.loon.framework.android.game.action.sprite.Picture;
import org.loon.framework.android.game.core.graphics.opengl.GLEx;
import org.loon.framework.android.game.core.graphics.opengl.LTexture;

import android.util.Log;

public class Grid extends Picture {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Animation animation, a1, a2;

    private int type, xpos, ypos;

    private int data;

    public Grid(LTexture img) {
        super(img);
    }

    public Grid(int x, int y, int data) {
        super(x, y);
        xpos = x;
        ypos = y;
        this.setData(data);
        Log.w("G8LLK", "create grid [x:" + xpos + ",y:" + ypos + ",data:" + data + "]");
    }

    public int getXpos() {
        return xpos;
    }

    public int getYpos() {
        return ypos;
    }

    public boolean isPassable() {
        return data == -1;
    }

    public void createUI(GLEx g) {
        super.createUI(g);
        switch (type) {
        case 0:
            if (a1 == null) {
                a1 = Animation.getDefaultAnimation("assets/s.png", 3, 48, 48, 100);
            }
            animation = a1;
            break;
        case 2:
            if (a2 == null) {
                a2 = Animation.getDefaultAnimation("assets/s1.png", 48, 48, 100);
            }
            animation = a2;
            break;
        default:
            break;
        }

        if (animation == null) {
            return;
        }
        if (type == 0 || type == 2) {
            LTexture img = animation.getSpriteImage();
            if (img != null) {
                g.drawTexture(img, x() + (getWidth() - img.getWidth()) / 2, y() + (getHeight() - img.getHeight()) / 2);
            }
        }
    }

    public void update(long t) {
        super.update(t);
        if (animation != null) {
            animation.update(t);
        }
    }

    public void setBorder(int type) {
        this.type = type;

    }

    public void setData(int data) {
        this.data = data;
    }

    public int getData() {
        return data;
    }

    @Override
    public String toString() {
        return "[x:" + xpos + ",y:" + ypos + ",data:" + data + "]";
    }
}